/*
 * @FileName: BusinessValidator.java
 * 
 * @Author
 * 
 * @Description:
 * 
 * @Date 2016年1月11日 上午11:10:36
 * 
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.validator.admin;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

import cn.hpclub.server.model.Business;

/**
 * Class description goes here
 *
 * @author
 */
public class BusinessValidator extends Validator{

    final static String BUSINESS_NAME_MESSAGE = "businessNameMessages";
    final static String BUSINESS_CODE_MESSAGE = "businessCodeMessages";

    @Override
    protected void validate(Controller c){
        if(!Business.dao.checkUnique(c.getParaToInt(Business.ID), Business.BUSINESS_CODE,
                                     c.getPara(Business.BUSINESS_CODE))){
            addError(BUSINESS_CODE_MESSAGE, "业务代码已存在");
        }

        // if(!Business.dao.checkUnique(c.getParaToInt(Business.ID),
        // Business.BUSINESS_NAME,
        // c.getPara(Business.BUSINESS_NAME))){
        // addError(BUSINESS_NAME_MESSAGE, "业务名称已存在");
        //
        // }
        this.validateRequiredString(Business.BUSINESS_NAME, BUSINESS_NAME_MESSAGE, "请输入 业务名称");
        validateRequiredString(Business.BUSINESS_CODE, BUSINESS_CODE_MESSAGE, "请输入 业务代码(数字或字母)");
        validateRegex(Business.BUSINESS_CODE, "^[0-9a-zA-Z]*$", BUSINESS_CODE_MESSAGE, "请输入 业务代码(数字或字母)");
    }

    @Override
    protected void handleError(Controller c){
        c.keepPara();
        c.render("/admin/business_add.tpl");
    }
}
